#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
const int M = 1e4 + 10;
int f[N][N];
int a[M];
int n, m;
int main()
{
	// 多源最短路
	cin >> n >> m;
	memset(f, 0x3f, sizeof f);
	for (int i = 1;i <= m;i++)
	{
		cin >> a[i];
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			cin >> f[i][j];
		}
	}
	for (int k = 1;k <= n;k++)
	{
		for (int i = 1;i <= n;i++)
		{
			for (int j = 1;j <= n;j++)
			{
				f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
			}
		}
	}
	int ret = 0;
	for (int i = 1;i < m;i++)
	{
		ret += f[a[i]][a[i + 1]];
	}
	cout << ret << endl;
	return 0;
}